package com.qst.order.controller;

import com.qst.order.service.SystemMonitorService;
import com.qst.order.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统监控Controller
 */
@RestController
@RequestMapping("/api/monitor")
@PreAuthorize("hasRole('ADMIN')") // 仅管理员可访问
public class SystemMonitorController {

    @Autowired
    private SystemMonitorService monitorService;

    /**
     * 获取所有系统监控数据（前端定时30秒调用一次）
     */

    @GetMapping("/systemInfo")
    public Result<SystemMonitorService.SystemMonitorDTO> getSystemMonitorInfo() {
        try {
            SystemMonitorService.SystemMonitorDTO data = monitorService.getSystemMonitorData();
            return Result.success("获取监控数据成功", data);
        } catch (Exception e) {
            return Result.error("获取监控数据失败：" + e.getMessage());
        }
    }

    /**
     * 新增：清空RabbitMQ队列（接收前端请求）
     */
    @PostMapping("/purgeQueue")
    public Result<Void> purgeQueue(@RequestBody Map<String, String> request) {
        try {
            String queueName = request.get("queueName");
            if (queueName == null || queueName.isEmpty()) {
                return Result.error("队列名称不能为空");
            }
            monitorService.purgeQueue(queueName);
            return Result.success("队列清空成功", null);
        } catch (Exception e) {
            return Result.error("清空队列失败：" + e.getMessage());
        }
    }
}


